//
//  ENMineViewController.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/7/18.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "ENMineViewController.h"
#import "ENMineTableHeaderView.h"
#import "ENContactViewController.h"
#import "LPCMBProgressHUD.h"
#import "UserModel.h"
#import <BotBrainPods/BotBrainManager.h>

@interface ENMineViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ENMineViewController
{
    ENMineTableHeaderView *_mthView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self updateMineUI];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    self.tabBarController.navigationItem.rightBarButtonItem = nil;
}

- (void)initUI
{
    _mthView = XIBFromMainBundle(@"ENMineTableHeaderView");
    _mthView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 135);
    [self.view addSubview:_mthView];
//    _tableView.tableHeaderView = mthView;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)updateMineUI
{
    [self updateNavBar];
    
    if ([UserModel isLogin]) {
        [_mthView updateData:[UserModel sharedUserModel]];
        [self updateNavBar];
    }
}

- (void)updateNavBar
{
    self.tabBarController.navigationItem.title = @"我的";
    
    if ([UserModel isLogin]) {
        [self addLoginOutNavBtn];
    }else {
        self.tabBarController.navigationItem.rightBarButtonItem = nil;
    }
}

- (void)addLoginOutNavBtn
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:ColorWhite forState:UIControlStateNormal];
    [btn setTitle:@"退出" forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_PFSC_Medium(16);
    [btn addTarget:self action:@selector(loginOutBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.tabBarController.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)loginOutBtnAction
{
    [LPCTools showAlertWithViewController:self title:@"提示" message:@"确认退出登录吗？" leftBtnTitle:@"退出" rightBtnTitle:@"取消" actionBlock:^(BOOL isCancel) {
        if (!isCancel) { //退出登录
            //清理用户信息
            [UserModel clearUserData];
            //退出布本评论系统
            [[BotBrainManager defaultManager] logoutWithCallBack:^(NSError *error) {
                
            }];
            //更新登录头视图
            [_mthView updateData:nil];
            //更新右上角退出按钮
            [self updateNavBar];
            //展示给用户
            [LPCMBProgressHUD showSuccessWithText:@"退出登录"];
        }
    }];
}


#pragma mark - UITableViewDelegate、UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        /*
        case 0:
        {
            return 1;
        }
            break;
        case 1:
        {
            return 3;
        }
            break;
        case 2:
         */
        case 0:
        {
            return 5;
        }
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    switch (indexPath.section) {
            /*
        case 0:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
            }
            cell.imageView.image = ImageWithContentsOfFileDefault(@"mine_cellLogo_task");
            cell.textLabel.text = @"每日任务";
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
            break;
        case 1:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
            }
            cell.textLabel.text = @[@"评论文章三次",@"分享三次文章/视频",@"打开查看三个广告"][indexPath.row];
        }
             break;
             case 2:
             */
        case 0:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
            }
            cell.imageView.image = ImageWithContentsOfFileDefault((@[@"mine_cellLogo_message",@"mine_cellLogo_feedback",@"mine_cellLogo_clear",@"mine_cellLogo_declaration",@"mine_cellLogo_contact"][indexPath.row]));
            cell.textLabel.font = FONT_PFSC_Regular(16);
            cell.textLabel.textColor = COLOR_BLACK_Title;
            cell.textLabel.text = @[@"我的消息",@"留言反馈",@"清理缓存",@"本站声明",@"联系我们"][indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0: //我的消息
            {
                PushToVC(@"ENMyDynamicViewController");
            }
                break;
            case 1: //留言反馈
            {
                PushToVC(@"ENFeedbackViewController");
            }
                break;
            case 2: //清理缓存
            {
                //清理缓存 (模拟)
                LPCMBProgressHUD *hud = [LPCMBProgressHUD showHUDAddedTo:self.view animated:YES];
                double delayInSeconds = 1.0;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [hud hide];
                    [LPCMBProgressHUD showSuccessWithText:WPLocalizedString(@"清理成功") ];
                });
            }
                break;
            case 3: //本站声明
            {
                ENContactViewController * encVC = [[ENContactViewController alloc] init];
                encVC.isContact = NO;
                [self.navigationController pushViewController:encVC animated:YES];
            }
                break;
            case 4: //联系我们
            {
                ENContactViewController * encVC = [[ENContactViewController alloc] init];
                encVC.isContact = YES;
                [self.navigationController pushViewController:encVC animated:YES];
            }
                break;
            default:
                break;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 3)];
    view.backgroundColor = COLOR_FFFFFF(0xeef1f5);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    view.backgroundColor = COLOR_FFFFFF(0x999999);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.5;
}



@end
